<?php


namespace app\admin\validate;


use app\common\model\Users;
use think\Validate;

class AddUser extends BaseValidate
{
    protected $rule = [
        'mobile'                    =>  'require|regex:mobile|unique:users',
        'realname'                  =>'require|chs',
        'password'                  =>  'require|min:6',
        'conpassword'               =>  'require|confirm:password',
        'rekey'                     =>  'checkRekey',
    ];
    protected $message = [
        'mobile.require'            => '请输入手机号码',
        'mobile.regex'              => '手机号码格式有误',
        'mobile.unique'             => '手机号码已注册',
        'realname.require'          => '请输入姓名',
        'realname.chs'              => '姓名只能为中文',
        'password.require'          => '请输入登录密码',
        'password.min'              => '登录密码至少6位',
        'conpassword.require'       => '请输入确认密码',
        'conpassword.confirm'       => '两次密码输入不一致',
        'rekey.require'             => '请输入推荐人ID',
    ];
    //检查推荐人
    protected function checkReKey($value, $rule, $data)
    {
        if (empty($value)){
            return true;
        }
        $user=Users::where('user_id',$value)->find();
        if (empty($user)){
            return '推荐人不存在';
        }
        if ($user['is_lock']==1){
            return '推荐人状态异常';
        }
        if ($user['mobile']==$data['mobile']){
            return '推荐人不能为自己';
        }
        return true;
    }
}
